package com.userInfoService.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import com.userInfoService.entity.PropertyEntity;
@Service
public class EnvironmentService implements EnvironmentAware{
	private Environment environment;
	@Override
	public void setEnvironment(Environment environment) {
		this.environment = environment;
	}
	
	public List<PropertyEntity> getDBConfig() {
		RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment,"config-server.database.");
		String dsPrefixs = propertyResolver.getProperty("datasource");
		if(dsPrefixs == null || "".equals(dsPrefixs))
			return null;
		List<PropertyEntity> propertyList = new ArrayList<PropertyEntity>();
		for(String dsPrefix:dsPrefixs.split(",")){
			PropertyEntity propertyEntity = new PropertyEntity();
			propertyEntity.setPrefix(dsPrefix);
			Map<String, Object> propertyMap = propertyResolver.getSubProperties(dsPrefix+".");
			propertyEntity.setProperties(propertyMap);
			propertyList.add(propertyEntity);
		}
		return propertyList;
	}
	
	public PropertyEntity getOtherConfigs() {
		RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(environment,"config-server.");
		PropertyEntity propertyEntity = new PropertyEntity();
		Map<String, Object> propertyMap = propertyResolver.getSubProperties("others.");
		propertyEntity.setPrefix("others");
		propertyEntity.setProperties(propertyMap);
		return propertyEntity;
	}
	
}
